package com.glsc.ngateway.common.api.filetransfer.resp;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.glsc.ngateway.common.api.common.enums.AppEhrStatus;
import com.glsc.ngateway.common.api.common.ex.GatewayException;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class FileTransferResponse<T> implements Serializable {
    private int status;
    private String msg;
    private T data;

    @ApiModelProperty(value = "fileId")
    private String fileId;

    public FileTransferResponse() {
    }

    public FileTransferResponse(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public T tryResult() {
        if (AppEhrStatus.Success.getErrorCode() == status) {
            return data;
        } else {
            throw GatewayException.error("file-transfer远程接口调用异常");
        }
    }


    public String getFileId() {
        return fileId;
    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
    }

    public static <T> FileTransferResponse error(String msg, T data) {
        return new FileTransferResponse(-1, msg, data);
    }

    public static <T> FileTransferResponse failError(String msg, T data) {
        return new FileTransferResponse(-2, msg, data);
    }

    public static FileTransferResponse error(String msg) {
        return new FileTransferResponse(-1, msg, null);
    }

    public static <T> FileTransferResponse success(String msg, T data) {
        return new FileTransferResponse(0, msg, data);
    }

    public static <T> FileTransferResponse success(T data) {
        return new FileTransferResponse(0, "ok", data);
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "FileTransferResponse{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
